package org.jmh.gift.controller;

import org.jmh.gift.dto.AddDTO;
import org.jmh.gift.dto.DeleteDTO;
import org.jmh.gift.dto.UpdateCountDTO;
import org.jmh.gift.dto.UpdateSelDTO;
import org.jmh.gift.service.IShopCarService;
import org.jmh.gift.domain.ShopCar;
import com.baomidou.mybatisplus.plugins.Page;
import org.jmh.gift.query.ShopCarQuery;
import org.jmh.gift.util.AjaxResult;
import org.jmh.gift.util.PageList;
import org.jmh.gift.util.TenantBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopCar")
public class ShopCarController {
    @Autowired
    public IShopCarService shopCarService;

    /**
     * 保存和修改公用的
     * @param shopCar  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ShopCar shopCar){

            if( shopCar.getId()!=null)
                shopCarService.updateById(shopCar);
            else
                shopCarService.insert(shopCar);
            return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
            shopCarService.deleteById(id);
            return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
            ShopCar shopCar = shopCarService.selectById(id);
            return AjaxResult.success(shopCar);

    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

            List< ShopCar> list = shopCarService.selectList(null);
            return AjaxResult.success(list);

    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody ShopCarQuery query)
    {

        Page<ShopCar> page = new Page<ShopCar>(query.getPage(),query.getRows());
        page = shopCarService.selectPage(page);
        PageList pageList = new PageList<ShopCar>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);

    }

    @PutMapping("/sel")
    public AjaxResult updateSelect(@Valid @RequestBody UpdateSelDTO dto){
        shopCarService.updateSel(dto);
        return AjaxResult.success();
    }

    //购物车新增
    @PostMapping("/add")
    public AjaxResult insert(@Valid @RequestBody AddDTO dto){
        shopCarService.add(dto);
        return AjaxResult.success();
    }

    //购物车修改
    @PutMapping("/count")
    public AjaxResult updateCount(@Valid @RequestBody UpdateCountDTO dto){
        shopCarService.updateCount(dto);
        return AjaxResult.success();
    }
    //购物车删除
    @DeleteMapping("/del")
    public AjaxResult deleteBySkuId(@Valid @RequestBody DeleteDTO dto){
        shopCarService.deleteBySkuId(dto);
        return AjaxResult.success();
    }
    //购物车查询
    @GetMapping("/map/{userId}")
    public AjaxResult getByUserId(@PathVariable("userId") Long id){
        Map<TenantBase,List<ShopCar>> lists = shopCarService.getByUserId(id);
        return AjaxResult.success(lists);
    }

}
